﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB.YJKControls;

namespace YJKRoom.NumberRoom
{
	public partial class VolumeNumberForm : System.Windows.Forms.Form
	{
		public string RoomTagFirstName
		{
			get
			{
				return this.tbFirstName.Text;
			}
			set
			{
				this.tbFirstName.Text = value;
			}
		}

		public int RoomTagNumber
		{
			get
			{
				return Convert.ToInt32(this.tbRoomNumber.Text);
			}
			set
			{
				this.tbRoomNumber.Text = value.ToString();
			}
		}

		public string RoomTagSecondName
		{
			get
			{
				return this.tbSecondName.Text;
			}
			set
			{
				this.tbSecondName.Text = value;
			}
		}

		public int Angle
		{
			get
			{
				return Convert.ToInt32(this.tbAngle.Text);
			}
			set
			{
				this.tbAngle.Text = value.ToString();
			}
		}

		public int SortWay
		{
			get
			{
				return this.cmbSortWay.SelectedIndex;
			}
			set
			{
				this.cmbSortWay.SelectedIndex = value;
			}
		}

		public VolumeNumberForm()
		{
			this.InitializeComponent();
			this.tbRoomNumber.Text = "1";
			this.tbFirstName.Text = "办公室";
			this.tbAngle.Text = "0";
			this.tbPreview.BorderStyle = BorderStyle.None;
			this.m_ToolTip.AutoPopDelay = 5000;
			this.m_ToolTip.ReshowDelay = 100;
			this.UpdatePreview();
		}

		private void UpdatePreview()
		{
			this.tbPreview.Text = this.tbFirstName.Text + this.tbRoomNumber.Text + this.tbSecondName.Text;
			this.m_ToolTip.SetToolTip(this.tbPreview, this.tbPreview.Text);
		}

		private void tbRoomNumber_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreview();
		}

		private void tbFirstName_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreview();
		}

		private void tbSecondName_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreview();
		}

		private void tbRoomNumber_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int.Parse(this.tbRoomNumber.Text);
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbAngle_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int.Parse(this.tbAngle.Text);
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个整数", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void VolumeNumberForm_Load(object sender, EventArgs e)
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "VolumeNumberForm.dat";
			if (!File.Exists(path))
			{
				this.cmbSortWay.SelectedIndex = 0;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.tbFirstName.Text = streamReader.ReadLine();
			this.tbSecondName.Text = streamReader.ReadLine();
			this.tbAngle.Text = streamReader.ReadLine();
			this.cmbSortWay.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
			this.UpdatePreview();
		}

		private void VolumeNumberForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "VolumeNumberForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.tbFirstName.Text);
			streamWriter.WriteLine(this.tbSecondName.Text);
			streamWriter.WriteLine(this.tbAngle.Text);
			streamWriter.WriteLine(this.cmbSortWay.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void lbAngle_MouseMove(object sender, MouseEventArgs e)
		{
			this.ShowToolTip(this.lbAngle, "X夹角：指定角度（一般为开间方向与X轴正向的夹角），\r\n根据开间/进深方向排列房间编号的顺序");
		}

		private void tbAngle_MouseMove(object sender, MouseEventArgs e)
		{
			this.ShowToolTip(this.lbAngle, "X夹角：指定角度（一般为开间方向与X轴正向的夹角），\r\n根据开间/进深方向排列房间编号的顺序");
		}

		private void label6_MouseMove(object sender, MouseEventArgs e)
		{
			this.ShowToolTip(this.label6, "顺序：箭头方向代表排列方向，先后顺序代表水平/竖直方向的优先关系，\r\n例如：→↑代表从左向右、从下向上，水平方向优先于竖直方向");
		}

		private void cmbSortWay_MouseMove(object sender, MouseEventArgs e)
		{
			this.ShowToolTip(this.cmbSortWay, "顺序：箭头方向代表排列方向，先后顺序代表水平/竖直方向的优先关系，\r\n例如：→↑代表从左向右、从下向上，水平方向优先于竖直方向");
		}

		private void ShowToolTip(Control ctrl, string text)
		{
			if (this.m_ToolTipCurrentCtrl == ctrl)
			{
				return;
			}
			this.m_ToolTipCurrentCtrl = ctrl;
			this.m_ToolTip.SetToolTip(ctrl, text);
		}

		private ToolTip m_ToolTip = new ToolTip();

		private Control m_ToolTipCurrentCtrl;
	}
}
